import type { AwardItemData } from './interface';

/**
 * 奖项屏幕
 */
export class AwardScreen {
    /**
     * 屏幕
     */
    private screen: UiScreen;
    /**
     * 背景
     */
    private background: UiBox;
    /**
     * 奖状框
     */
    private box: UiImage;
    /**
     * 奖项标题
     */
    private title: UiText;
    /**
     * 获奖作品名
     */
    private itemTitle: UiText;
    /**
     * 获奖作品图片
     */
    private itemImage: UiImage;
    /**
     * 获奖作品作者&协作者
     */
    private itemAuthor: UiText;

    /**
     * 主办名
     */
    private hostName: UiText;

    constructor() {
        this.screen = UiScreen.create();
        this.screen.visible = false;

        this.background = UiBox.create();
        this.background.parent = this.screen;
        this.background.size.offset.copy(Vec2.create({ x: 0, y: 0 }));
        this.background.size.scale.copy(Vec2.create({ x: 1, y: 1 }));
        this.background.backgroundColor.copy(Vec3.create({ r: 0, g: 0, b: 0 }));
        this.background.backgroundOpacity = 0.5;
        this.background.pointerEventBehavior =
            PointerEventBehavior.DISABLE_AND_BLOCK_PASS_THROUGH;

        const size: Vec2 = Vec2.create({
            x: screenWidth * 0.8,
            y: screenHeight * 0.8,
        });

        this.box = UiImage.create();
        this.box.parent = this.screen;
        this.box.size.offset.copy(size);
        this.box.position.offset.copy(
            Vec2.create({ x: -size.x / 2, y: -size.y / 2 })
        );
        this.box.position.scale.copy(Vec2.create({ x: 0.5, y: 0.5 }));
        this.box.backgroundOpacity = 0;
        this.box.image = 'picture/奖状框架.jpg';
        this.box.imageDisplayMode = ImageDisplayMode.Contain;

        this.title = UiText.create();
        this.title.parent = this.box;
        this.title.size.offset.copy(
            Vec2.create({ x: size.x * 0.8, y: size.y * 0.2 })
        );
        this.title.position.offset.copy(
            Vec2.create({ x: size.x * 0.1, y: size.y * 0.4 })
        );
        this.title.textFontSize = size.x / 15;
        // this.title.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
        this.title.textXAlignment = 'Center';
        this.title.textYAlignment = 'Center';

        this.itemTitle = UiText.create();
        this.itemTitle.parent = this.box;
        this.itemTitle.size.offset.copy(
            Vec2.create({ x: size.x * 0.8, y: size.y * 0.1 })
        );
        this.itemTitle.position.offset.copy(
            Vec2.create({ x: size.x * 0.1, y: size.y * 0.1 })
        );
        this.itemTitle.textFontSize = size.x / 20;
        // this.title.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
        this.itemTitle.textXAlignment = 'Center';
        this.itemTitle.textYAlignment = 'Center';

        this.itemImage = UiImage.create();
        this.itemImage.parent = this.box;
        this.itemImage.size.offset.copy(
            Vec2.create({ x: size.x * 0.2, y: size.x * 0.2 })
        );
        this.itemImage.position.offset.copy(
            Vec2.create({ x: size.x * 0.4, y: size.y * 0.6 })
        );

        this.itemAuthor = UiText.create();
        this.itemAuthor.parent = this.box;
        this.itemAuthor.size.offset.copy(
            Vec2.create({ x: size.x * 0.8, y: size.y * 0.1 })
        );
        this.itemAuthor.position.offset.copy(
            Vec2.create({ x: size.x * 0.1, y: size.y * 0.2 })
        );
        this.itemAuthor.textFontSize = size.x / 20;
        // this.title.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
        this.itemAuthor.textXAlignment = 'Center';
        this.itemAuthor.textYAlignment = 'Center';

        this.hostName = UiText.create();
        this.hostName.parent = this.box;
        this.hostName.size.offset.copy(
            Vec2.create({
                x: size.x / 5,
                y: size.y / 5,
            })
        );
        this.hostName.position.offset.copy(
            Vec2.create({
                x: size.x - this.hostName.size.offset.x,
                y: size.y - this.hostName.size.offset.y,
            })
        );
        this.hostName.textFontSize = size.x / 46;
        this.hostName.textContent = 'shen';
    }

    /**
     * 显示
     */
    show(): void {
        this.screen.zIndex = 100;
        this.screen.visible = true;
    }

    /**
     * 隐藏
     */
    hide(): void {
        this.screen.visible = false;
    }

    /**
     * 更新奖项内容
     * @param award 奖项数据
     */
    update(award: AwardItemData | string) {
        if (typeof award === 'object') {
            this.title.textContent = award.award;
            this.itemTitle.textContent = award.title;
            this.itemImage.image = award.image;
            this.itemAuthor.textContent = award.author.join('、');
            this.hostName.visible = false;
            this.box.image = 'picture/奖状框架.jpg';
        } else if (typeof award === 'string') {
            this.title.visible = false;
            this.itemTitle.visible = false;
            this.itemImage.visible = false;
            this.itemAuthor.visible = false;
            this.hostName.visible = false;
            this.box.image = award;
        }
    }
}

export const awardScreen = new AwardScreen();
